public class 最大子数组和 {
    //https://leetcode.cn/problems/maximum-subarray/description/?envType=problem-list-v2&envId=array
    public int maxSubArray(int[] nums) {
        //前i个元素的最大和
        int n = nums.length;
        int[] dp = new int[n + 1];
        int ans = -0x3f3f3f3f;
        for(int i = 1;i <= n;i++){
            //和前面的元素结合或者只选择自己哪个大
            dp[i] = Math.max(dp[i - 1] + nums[i - 1],nums[i - 1]);
            ans = Math.max(dp[i],ans);
        }
        return ans;
    }
}
